package com.goodlearning.gloriamobile.adapter;

import java.util.List;
import java.util.Map;

import com.goodlearning.gloriamobile.adapter.CustomAdapter.IListItemController;
import com.goodlearning.gloriamobile.commons.Utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

/**
 * Adapter 상위 객체 정의 Object 로 형 정의 했다 각 객체에서 사용되는 객체가 워낙 다양함으로
 * 
 * @author rapty54
 * 
 * @param <T1>
 */
public class CustomAdapter<T1> extends ArrayAdapter<T1> {
	
	private LayoutInflater inflater;
	private int textViewResourceId;
	private Context context;
	private List<T1> items;
	private IListItemController controler;
	/**
	 * 3 번째 List Object 정의 형 정의 히여 넘길 객체 ArrayList 로 담는다. 
	 * @param context
	 * @param textViewResourceId
	 * @param objects
	 * @param controller
	 */
	public CustomAdapter(Context context, int textViewResourceId,List<T1> objects, IListItemController controller) {
		super(context, textViewResourceId, objects);
		this.context = context;
		this.textViewResourceId = textViewResourceId;
		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.items = objects;
		this.controler = controller;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = null;
		if (convertView != null)
			v = convertView;
		else
			v = inflater.inflate(textViewResourceId, null);
		Utils.log("View:" + controler + "position:" + position);
		return controler == null ? v : controler.controlView(items, position,v, parent);
	}

	public interface IListItemController<T1> {

		/**
		 * 리스트뷰의 getView호출시의 작업을 대신한다. !! null 리턴하면 안된다 주의하자~~~~~~
		 * 
		 * @param position
		 * @param convertView
		 * @param parent
		 * @return
		 */
		public View controlView(List<T1> items, int position, View convertView,
				ViewGroup parent);
	}
}
